# -*- coding: utf-8 -*-
from common.common_util import *

def get_hwnds(id):
    '''
    根据窗口标题获取对应句柄值
    :param id: 含有某个字段
    :return:
    '''
    hwnds = get_all_windows("梦幻西游")
    for i in range(len(hwnds)):
        title = win32gui.GetWindowText(hwnds[i])
        if f"[{id}]" in title:
            hwnd = hwnds[i]
            return hwnd

def get_postion(hwnd):
    '''
    根据句柄获取窗口位置
    :param hwnd: 句柄值
    :return:
    '''

    # 获取窗口左上角坐标和右下角坐标
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    # 输出坐标信息
    print('窗口左上角坐标：({}, {})'.format(left, top))
    print('窗口右下角坐标：({}, {})'.format(right, bottom))
    return left, top,right, bottom
def windows_swipe_zg(pc_sreen,posc_number,length):
    '''
    将窗口移动到指定为止，posc_number = [row,col]
    :param pc_sreen: pc分辨率（1920,1080）
    :param posc_number: 位置编号 [row,col]
    :param length: 缩放后的长度
    :return:
    '''
    hwnd  = get_hwnds("txt")
    connect_device(f'Windows:///{hwnd}')
    left, top, right, bottom = get_postion(hwnd)
    # 窗口移动右上角，方便缩放
    start_x = (left + right)/2
    start_y = top + 20
    end_x = (right - left)/2
    end_y = 20
    connect_device(f'Windows:///')
    swipe((start_x,start_y),(end_x,end_y),duration=1, steps=random.randint(4,10))

    # 缩放操作
    sleep(2)
    left, top, right, bottom = get_postion(hwnd)
    start_x = right-8
    start_y = (top + bottom)/2
    end_x = left + length
    end_y = start_y # 根据x缩放进行缩放
    swipe((start_x, start_y), (end_x, end_y), duration=2, steps=random.randint(4, 10))


    # 移动操作
    sleep(2)
    left, top, right, bottom = get_postion(hwnd)
    start_x = (left + right)/2
    start_y = top + 20
    end_x = (left + right)/2 + (right-left)*posc_number[1]
    end_y = top/2 + (bottom-top)*posc_number[0]
    if end_y > pc_sreen[1]:
        end_y = pc_sreen[1] - 40 - (right-left)
    swipe((start_x,start_y),(end_x,end_y), duration=2, steps=random.randint(4, 10))
def creat_json_data(device_titles,expect=3):
    '''
    根据设备数量，创建存放记录数据
    :param device_titles:
    :return:
    '''
    json_data = []
    for i in range(len(device_titles)):
        data = {"dev":device_titles[i],
                "hwnd":get_hwnds(device_titles[i]),
                "expect":expect,
                "actual":0
                }
        json_data.append(data)
    return json_data
def GhostCatching(hwnd):
    '''
    指定捉鬼设备，并开始捉鬼，并返回是否开始第2轮
    :param hwnd:
    :return:
    '''
    start_do_in(hwnd)
    connect_device(f'Windows:///{hwnd}')
    try:
        # 等待弹窗 做2次任务
        wait(Template(r"tpl1711162580677.png", record_pos=(0.102, 0.037), resolution=(635, 503)), 10)
        pos = wait(Template(r"tpl1711162586493.png", record_pos=(0.098, 0.046), resolution=(635, 503)))
        x = random.randint(-20, 20)
        y = random.randint(-5, 5)
        sleep(1)
        touch((pos[0] + x, pos[1] + y), times=1)
        start_do_in(hwnd)
        return True
    except Exception as e:
        print(e)
        return False
def start_do_in(hwnd):
    print(f"连接设备的句柄值：{hwnd}")
    connect_device(f'Windows:///{hwnd}')
    try:
        # 点击弹窗中的“捉鬼任务”
        if exists(Template(r"tpl1711162604668.png", record_pos=(0.335, -0.102), resolution=(635, 503))):
            pos = wait(Template(r"tpl1711162604668.png", record_pos=(0.33, -0.086), resolution=(635, 503)))
            x = random.randint(-20, 20)
            y = random.randint(-5, 5)
            touch((pos[0] + x, pos[1] + y), times=1)
            sleep(1)
        if exists(Template(r"tpl1723946417330.png", record_pos=(0.332, -0.022), resolution=(494, 398))):
            pos = wait(Template(r"tpl1723946417330.png", record_pos=(0.332, -0.022), resolution=(494, 398)))
            x = random.randint(-20, 20)
            y = random.randint(-5, 5)
            touch((pos[0] + x, pos[1] + y-30), times=1)
            sleep(1)
        # 点击任务列表中的 “捉鬼任务
        if exists(Template(r"tpl1711162801261.png", record_pos=(0.336, -0.204), resolution=(635, 503))):
            pos = wait(Template(r"tpl1711162801261.png", record_pos=(0.336, -0.204), resolution=(635, 503)))
            x = random.randint(-20, 20)
            y = random.randint(-5, 5)
            touch((pos[0] + x, pos[1] + 20 + y), times=1)


    except Exception as e:
        print(e)
def quit_condit(json_data):
    #判断结束程序条件,期望值与实际值相等，就结束执行
    ql = True
    for i in range(len(json_data)):
        fl = json_data[i]['expect'] == json_data[i]['actual']
        ql = ql & fl
    return ql
def run_mask(device_titles,exp_counter):
    #minimize_pycham()  # 最小化pycharm
    json_data = creat_json_data(device_titles,exp_counter)
    print(json_data)
    while True:
        for i in range(len(json_data)):
            if json_data[i]['expect'] > json_data[i]['actual']:
                flag = GhostCatching(json_data[i]["hwnd"])
                if flag:
                    json_data[i]["actual"] = json_data[i]["actual"]+1
        print(json_data)
        ql = quit_condit(json_data)
        if ql:
            break
if __name__ == '__main__':
    device_titles = [5,9]
    exp_counter = 6
    run_mask(device_titles,exp_counter)
